package org.jeecg.modules.knowledgerelation.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.knowledgerelation.KnowledgeTreeVO;
import org.jeecg.modules.knowledgerelation.entity.KnowledgeRelation;
import org.jeecg.modules.knowledgerelation.service.IKnowledgeRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 知识之间的关系
 * @Author: jeecg-boot
 * @Date:   2020-12-20
 * @Version: V1.0
 */
@Api(tags="知识之间的关系")
@RestController
@RequestMapping("/knowledgeRelation")
@Slf4j
public class KnowledgeRelationController extends JeecgController<KnowledgeRelation, IKnowledgeRelationService> {
	@Autowired
	private IKnowledgeRelationService knowledgeRelationService;
	
	/**
	 * 分页列表查询
	 *
	 * @param knowledgeRelation
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "知识之间的关系-分页列表查询")
	@ApiOperation(value="知识之间的关系-分页列表查询", notes="知识之间的关系-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(KnowledgeRelation knowledgeRelation,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		Page<KnowledgeRelation> page = new Page<KnowledgeRelation>(pageNo, pageSize);
		IPage<KnowledgeRelation> list = knowledgeRelationService.mypage(page,req);
		List<KnowledgeRelation> lis = list.getRecords().
				stream().
				sorted(Comparator.comparing(KnowledgeRelation::getFromTitle).reversed()).
				collect(Collectors.toList());
		list.setRecords(lis);
		return Result.OK(list);
	}
	
	/**
	 *   添加
	 *
	 * @param knowledgeRelation
	 * @return
	 */
	@AutoLog(value = "知识之间的关系-添加")
	@ApiOperation(value="知识之间的关系-添加", notes="知识之间的关系-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody KnowledgeRelation knowledgeRelation) {
		knowledgeRelationService.save(knowledgeRelation);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param knowledgeRelation
	 * @return
	 */
	@AutoLog(value = "知识之间的关系-编辑")
	@ApiOperation(value="知识之间的关系-编辑", notes="知识之间的关系-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody KnowledgeRelation knowledgeRelation) {
		knowledgeRelationService.updateById(knowledgeRelation);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "知识之间的关系-通过id删除")
	@ApiOperation(value="知识之间的关系-通过id删除", notes="知识之间的关系-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		knowledgeRelationService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "知识之间的关系-批量删除")
	@ApiOperation(value="知识之间的关系-批量删除", notes="知识之间的关系-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.knowledgeRelationService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "知识之间的关系-通过id查询")
	@ApiOperation(value="知识之间的关系-通过id查询", notes="知识之间的关系-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		KnowledgeRelation knowledgeRelation = knowledgeRelationService.getById(id);
		if(knowledgeRelation==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(knowledgeRelation);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param knowledgeRelation
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, KnowledgeRelation knowledgeRelation) {
        return super.exportXls(request, knowledgeRelation, KnowledgeRelation.class, "知识之间的关系");
    }

   /**
    * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, KnowledgeRelation.class);
    }


	/**
	 * app获取知识树
	 *
	 * @return
	 */
	@AutoLog(value = "知识之间的关系-app获取知识树")
	@ApiOperation(value="知识之间的关系-app获取知识树", notes="知识之间的关系-app获取知识树")
	@GetMapping(value = "/getTree")
	public Result<?> getTree() {
		List<KnowledgeTreeVO> list = knowledgeRelationService.getTree();
		return Result.OK(list);
	}

}
